package com.ac.scum.log.web.model.server.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.exception.config.ExceptionType;
import com.ac.common.core.result.Result;
import com.ac.scum.config.bean.ServerFTPFileConfigAdmin;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.web.model.player.service.PlayerService;
import com.ac.scum.log.web.model.server.service.ServerAdminConfigService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("server/config/admin")
public class ServerAdminConfigController {

    private final ServerAdminConfigService serverAdminConfigService;
    private final PlayerService playerService;


    @GetMapping("page/{current}/{size}")
    public Result<Page<ServerFTPFileConfigAdmin>> page(@PathVariable("current") Integer current, @PathVariable("size") Integer size) throws BaseException {
        Page<ServerFTPFileConfigAdmin> page = serverAdminConfigService.page(new Page<>(current, size), Wrappers.lambdaQuery(ServerFTPFileConfigAdmin.class).eq(ServerFTPFileConfigAdmin::getServerId, CurrentUserUtil.getCurrentServerId()));
        List<Player> players = playerService.getBaseMapper().findPlayerByServerIdAndSteamIds(CurrentUserUtil.getCurrentServerId(), page.getRecords().stream().map(ServerFTPFileConfigAdmin::getSteamId).toList());
        Map<Long, String> playerMap = players.stream()
                .collect(Collectors.toMap(Player::getSteamId, player -> player.getName() != null ? player.getName() : "-"));
        List<ServerFTPFileConfigAdmin> records = page.getRecords();
        for (ServerFTPFileConfigAdmin record : records) {
            record.setPlayerName(playerMap.get(record.getSteamId()));
        }
        page.setRecords(records);
        return Result.ok(page);
    }

    @PostMapping("updateById")
    public Result<Boolean> updateById(@RequestBody ServerFTPFileConfigAdmin serverFTPFileConfigAdmin) throws BaseException {
        Assert.notNull(serverFTPFileConfigAdmin, "不可为空");
        Assert.notNull(serverFTPFileConfigAdmin.getId(), "id不可为空");

        ServerFTPFileConfigAdmin byId = serverAdminConfigService.getById(serverFTPFileConfigAdmin.getId());
        if (byId == null || !byId.getServerId().equals(CurrentUserUtil.getCurrentServerId())) {
            throw new BaseException(ExceptionType.Parameter_Error);
        }
        if (serverFTPFileConfigAdmin.getType() == 1 || serverFTPFileConfigAdmin.getType() == 3) {
            serverFTPFileConfigAdmin.setExpirationTime(LocalDateTime.now().plusYears(100));
        }
        serverFTPFileConfigAdmin.setSavedFlag(false);
        return Result.ok(serverAdminConfigService.updateById(serverFTPFileConfigAdmin));
    }

    @PutMapping("save")
    public Result<Boolean> save(@RequestBody ServerFTPFileConfigAdmin serverFTPFileConfigAdmin) throws BaseException {
        Assert.notNull(serverFTPFileConfigAdmin, "不可为空");
        Assert.notNull(serverFTPFileConfigAdmin.getType(), "Type不可为空");
        Assert.notNull(serverFTPFileConfigAdmin.getSteamId(), "SteamID不可为空");

        serverFTPFileConfigAdmin.setId(null);
        serverFTPFileConfigAdmin.setSavedFlag(false);
        serverFTPFileConfigAdmin.setServerId(CurrentUserUtil.getCurrentServerId());
        serverFTPFileConfigAdmin.setExpirationTime(serverFTPFileConfigAdmin.getType() == 1 ? LocalDateTime.now().plusYears(100) : serverFTPFileConfigAdmin.getExpirationTime() == null ? LocalDateTime.now() : serverFTPFileConfigAdmin.getExpirationTime());
        return Result.ok(serverAdminConfigService.save(serverFTPFileConfigAdmin));
    }
}
